package com.jxy.bean.constant;

import com.jxy.bean.ClassFile;
import com.jxy.tool.Tool;

import java.io.DataInputStream;

public class StringInfo extends ConstantPool {
    public int string_index;

    public StringInfo(DataInputStream dis, ClassFile cf) {
        super(ConstantPoolTag.CONSTANT_String.getTag(), cf);
        //长度u2
        string_index = Tool.readU2(dis);
    }

    public String getString() {
        if (valid()) {
            Utf8Info utf8Info = (Utf8Info)cf.getPools()[string_index];
            return utf8Info.getString();
        }
        return "string_index err !";
    }

    @Override
    public String toString() {
        return ConstantPoolTag.CONSTANT_String.getType() + "{tag:" + tag + ", string_index:" + string_index + "(" + getString() + ")}";
    }

    @Override
    public boolean valid() {
        //string_index必须指向CONSTANT_Utf8_info结构
        if (cf.getPools()[string_index].getTag() == ConstantPoolTag.CONSTANT_Utf8.getTag()) {
            return true;
        }
        return false;
    }
}
